home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / metertest / metertest.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.5 KB  |  95 lines

  1. /*
  2.     File:        MeterTest.c
  3.  
  4.     Contains:    This snippet demonstrates record metering through the use of SPBGetDeviceInfo() 
  5.                 and SPBSetDeviceInfo() using the siLevelMeterOnOff selector. The code calls 
  6.                 SPBSetDeviceInfo() to initially set metering to off, then does a quick sampling of 
  7.                 sound input using SPBGetDeviceInfo(). Metering is then turned on and sampling is 
  8.                 repeated. This is an SIOW application and could be done more elegantly using a
  9.                 graphical representation for sound input levels. But you get the idea. . .        
  10.  
  11.     Written by: Kevin Mellander    
  12.  
  13.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  14.  
  15.                 You may incorporate this Apple sample source code into your program(s) without
  16.                 restriction. This Apple sample source code has been provided "AS IS" and the
  17.                 responsibility for its operation is yours. You are not permitted to redistribute
  18.                 this Apple sample source code as "Apple sample source code" after having made
  19.                 changes. If you're going to re-distribute the source, we require that you make
  20.                 it clear in the source that the code was descended from Apple sample source
  21.                 code, but that you've made changes.
  22.  
  23.     Change History (most recent first):
  24.                 7/29/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  25.                 
  26.  
  27. */
  28. #include <Sound.h>
  29. #include <SoundInput.h>
  30. #include <ToolUtils.h>
  31. #include <Types.h>
  32. #include <stdio.h>
  33.  
  34. struct soundLevelData{
  35.         short meterState;
  36.         short meterSetting;
  37.     } getSoundLevelInfo;
  38.  
  39. void main()
  40. {
  41.     Str255    deviceString;
  42.     OSErr    soundErr = 0;
  43.     long    mySIRefNum;
  44.     short    sampletime = 0;
  45.     short    setSoundMeterState = 1;
  46.     
  47.     getSoundLevelInfo.meterState = 0;
  48.     getSoundLevelInfo.meterSetting = 0;
  49.     
  50.     deviceString[0] = 0;
  51.         
  52.     //Open the sound input device
  53.     soundErr = SPBOpenDevice(deviceString,siWritePermission,&mySIRefNum);
  54.     if (soundErr != noErr)
  55.         DebugStr("\p Failure at call to SPBOpenDevice");
  56.         
  57.     //Set the initial state of the sound input meter to zero just to be sure we start in an "off" state--after all this *is* a test    
  58.     soundErr = SPBSetDeviceInfo(mySIRefNum,siLevelMeterOnOff,(Ptr) &getSoundLevelInfo.meterState);
  59.     if (soundErr != noErr)
  60.         DebugStr("\p Failure at call to SPBSetDeviceInfo set to 0");
  61.  
  62.     //Monitor sound input and make sure everything is really zeroed
  63.     for (sampletime = 0; sampletime<=100; ++sampletime)
  64.     {
  65.         soundErr = SPBGetDeviceInfo(mySIRefNum,siLevelMeterOnOff, (Ptr) &getSoundLevelInfo);
  66.         if (soundErr != noErr)
  67.             DebugStr("\p Failure at call to SPBGetDeviceInfo");
  68.         else
  69.             printf("The GetDeviceInfo meter state is %d and the meter setting is %d\n",getSoundLevelInfo.meterState,getSoundLevelInfo.meterSetting);
  70.     }
  71.     
  72.     //Turn the sound input meter on
  73.     soundErr = SPBSetDeviceInfo(mySIRefNum,siLevelMeterOnOff,(Ptr) &getSoundLevelInfo.meterState);
  74.     if (soundErr != noErr)
  75.         DebugStr("\p Failure at call to SPBSetDeviceInfo set to 1");
  76.     else 
  77.         printf("\nThe call to SPBGetDeviceInfo was successful;\nThe SetDeviceInfo meter state was set to %i\n\n",setSoundMeterState);
  78.         
  79.     
  80.     //Monitor sound input again with meters on 
  81.     for (sampletime = 0; sampletime<=100; ++sampletime)
  82.     {
  83.         soundErr = SPBGetDeviceInfo(mySIRefNum,siLevelMeterOnOff, (Ptr) &getSoundLevelInfo);
  84.         if (soundErr != noErr)
  85.             DebugStr("\p Failure at call to SPBGetDeviceInfo");
  86.         else
  87.             printf("The GetDeviceInfo meter state is %d and the meter setting is %d\n",getSoundLevelInfo.meterState,getSoundLevelInfo.meterSetting);
  88.         
  89.     }
  90.  
  91.     //We're done so close the device
  92.     soundErr = SPBCloseDevice(mySIRefNum);
  93.     if (soundErr != noErr)
  94.         DebugStr("\p Failure at call to SPBCloseDevice");
  95. }